package gspushmq.v1.produce.job.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArraySet;

import org.apache.commons.lang3.StringUtils;
import org.quartz.DisallowConcurrentExecution;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import gspushmq.model.ApiMessage;
import gspushmq.model.ApiMessageType;
import gspushmq.util.GGLogger;
import gspushmq.util.SynUtil;
import gspushmq.v1.produce.GGDataStorage;
import gspushmq.v1.produce.job.core.GGServiceJob;
import gspushmq.v1.subscribe.GGSubscribe;
import gspushmq.v1.subscribe.send.core.GGMessageSend;

/**
 *
 * 生产api更新数据的统一入口
 * @version 1.0
 * @since JDK1.7
 * @author yutao
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2016 SunTime Co'Ltd Inc.All rights reserved.
 * @date 2018年11月15日上午11:12:55
 */
@DisallowConcurrentExecution
public class GGProduceApiJob extends GGServiceJob {

	@Override
	protected String setJobName(Map<String, Object> params) {
		return "生产api更新的数据";
	}
	
	@Override
	public void doServiceJob(Map<String, Object> args) {
		
		Iterator<Entry<ApiMessageType, ConcurrentSkipListSet<ApiMessage>>> iterator = GGSubscribe.ApiUrlMessage.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry<ApiMessageType, ConcurrentSkipListSet<ApiMessage>> o = iterator.next();
			
			ApiMessageType apiType = o.getKey();
			
			
			Set<ApiMessage> value = o.getValue();
			
			if(value==null || value.isEmpty()) {
				iterator.remove();
				continue;
			}
			
			JSONArray array = GGDataStorage.ApiListMap.get(apiType);
			if(array == null) {
				array = new JSONArray();
				GGDataStorage.ApiListMap.put(apiType, array);
			}
			array.clear();
			String apiUrl = apiType.getApi_url();
			if("v1/tg/get_ai_event_catalysts_list".equals(apiUrl)) {
				
				Map<String, String> paramsMap = new HashMap<>();
				paramsMap.put("is_push", "1");
				paramsMap.put("checkbox_code", "all");
				paramsMap.put("ignore_mystock", "1");
				
				JSONObject jsonObject = GGMessageSend.commonUrl(paramsMap, apiUrl);
				if(jsonObject == null) {
					continue;
				}
				JSONArray jsonArray = jsonObject.getJSONArray("data");
				if(jsonArray != null) {
					array.addAll(jsonArray);
					GGSubscribe.apiHandlerQueue.add(apiType);
				}
			}else {
				List<String> accountList = new ArrayList<>();
				
				for(ApiMessage apiMess : value) {
					String accountId = apiMess.getAccount_id();
					
					Map<String, Object> baseAidMap = SynUtil.synApiCommon(apiType);
					String messageFlag = apiType.getMessageFlag();
					String baseKey = accountId + "|" + messageFlag;
					
					Object baseObj = baseAidMap.get(baseKey);
					if (baseObj == null) {
						GGLogger.warn(getClass(), "----第一次订阅还没有推送的数据不需要正常job推送----" + apiMess.getRequest_id() + accountId + "---wst---" + apiMess.getWst().isOpen());
						continue;
					}
					GGLogger.warn(getClass(), "----正常job---apiMess----" + apiMess.getRequest_id() + accountId + "---wst---" + apiMess.getWst().isOpen());
					accountList.add(accountId);
				}
				
				String accountIds = StringUtils.join(accountList, ";");
				Map<String, String> paramsMap = new HashMap<>();
				paramsMap.put("account_ids", accountIds);
				JSONObject jsonObject = GGMessageSend.commonUrl(paramsMap, apiUrl);
				 Object jsonObj = jsonObject.get("data");
				 if(jsonObj != null) {
					 array.add(jsonObj);
				 }
				 GGSubscribe.apiHandlerQueue.add(apiType);
			}
		}
	}

	
}
